/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.quartz.ee.servlet.QuartzInitializerServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.Log4jConfigListener;

import lombok.extern.log4j.Log4j;

/**
 * @author Mht
 * @date May 10, 2016
 */
@Log4j
public class ProjectConfigInitializer implements WebApplicationInitializer 
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet
	 * .ServletContext)
	 */
	public void onStartup(ServletContext servletContext) throws ServletException {
		// TODO Auto-generated method stub
		initializeSpringConfig(servletContext);
		initializeLog4jConfig(servletContext);
		initializeSpringMVCConfig(servletContext);
		// initializeQuartzServlet(servletContext);
		servletContext.addListener(RequestContextListener.class);
	}

	@SuppressWarnings("deprecation")
	private void initializeLog4jConfig(ServletContext container) {
		// Log4jConfigListener
		container.setInitParameter("log4jConfigLocation", "classpath:conf/log4j.properties");
		container.addListener(Log4jConfigListener.class);
	}

	private void initializeSpringConfig(ServletContext container) {
		// Create the 'root' Spring application context
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(AppConfiguration.class);
		// Manage the life cycle of the root application context
		container.addListener(new ContextLoaderListener(rootContext));
		// container.setInitParameter("contextConfigLocation",
		// "configuration.RedisConfiguration");
	}

	private void initializeSpringMVCConfig(ServletContext container) {
		// Create the spring rest servlet's Spring application context
		AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
		dispatcherContext.register(RestServiceConfiguration.class);

		// Register and map the spring rest servlet
		ServletRegistration.Dynamic dispatcher = container.addServlet("SpringMVC",
				new DispatcherServlet(dispatcherContext));
		dispatcher.setLoadOnStartup(1);
		dispatcher.setAsyncSupported(true);
		dispatcher.addMapping("/springmvc/*");
	}

	private void initializeQuartzServlet(ServletContext container) {
		// Create the spring rest servlet's Spring application context
		AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
		dispatcherContext.register(QuartzInitializerServlet.class);

		// Register and map the spring rest servlet
		ServletRegistration.Dynamic dispatcher = container.addServlet("QuartzInitializer",
				new DispatcherServlet(dispatcherContext));
		dispatcher.setInitParameter("shutdown-on-unload", "true");
		dispatcher.setInitParameter("wait-on-shutdown", "false");
		dispatcher.setInitParameter("start-scheduler-on-load", "true");
		dispatcher.setInitParameter("config-file", "classpath:conf/quartz.properties");
		dispatcher.setLoadOnStartup(2);
		log.info("Initialize Quartz Servlet");
	}
}
